

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>rfml.data.converters.rml_2016 &mdash; RFML w/ PyTorch Software Documentation 1.0.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> RFML w/ PyTorch Software Documentation
          

          
          </a>

          
            
            
              <div class="version">
                1.0.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../data.html"> Data</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../nbutils.html"> Notebook Utilities</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../nn.html"> Neural Networks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../ptradio.html"> PyTorch Radio</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">RFML w/ PyTorch Software Documentation</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>rfml.data.converters.rml_2016</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for rfml.data.converters.rml_2016</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Data loaders for the RML2016.10x open source datasets provided by DeepSig, Inc.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Bryse Flowers &lt;brysef@vt.edu&gt;&quot;</span>

<span class="c1"># External Includes</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">defaultdict</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pickle</span>
<span class="kn">import</span> <span class="nn">tarfile</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="k">import</span> <span class="n">Tuple</span><span class="p">,</span> <span class="n">Dict</span>
<span class="kn">from</span> <span class="nn">urllib.request</span> <span class="k">import</span> <span class="n">urlretrieve</span>
<span class="kn">from</span> <span class="nn">warnings</span> <span class="k">import</span> <span class="n">warn</span>

<span class="c1"># Internal Includes</span>
<span class="kn">from</span> <span class="nn">rfml.data</span> <span class="k">import</span> <span class="n">Dataset</span><span class="p">,</span> <span class="n">DatasetBuilder</span>


<span class="k">class</span> <span class="nc">RML2016DataLoader</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span> <span class="n">cache_path</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">remote_url</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">unpickled_path</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">warning_msg</span><span class="p">:</span> <span class="nb">str</span>
    <span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">CACHE_PATH</span> <span class="o">=</span> <span class="n">cache_path</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">REMOTE_URL</span> <span class="o">=</span> <span class="n">remote_url</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">UNPICKLED_PATH</span> <span class="o">=</span> <span class="n">unpickled_path</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">WARNING_MSG</span> <span class="o">=</span> <span class="n">warning_msg</span>

    <span class="k">def</span> <span class="nf">load</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">path</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                    <span class="s2">&quot;If path is provided, it must actually exist.  Provided path: &quot;</span>
                    <span class="s2">&quot;</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
                <span class="p">)</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_load_local</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="n">path</span><span class="p">)</span>

        <span class="c1"># If this function has previously been called before to fetch the dataset from the</span>
        <span class="c1"># remote, then it will have already been cached locally and unpickled.</span>
        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">UNPICKLED_PATH</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_load_local</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">UNPICKLED_PATH</span><span class="p">)</span>

        <span class="n">warn</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">WARNING_MSG</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_download</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_load_local</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">UNPICKLED_PATH</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_load_local</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Dataset</span><span class="p">:</span>
        <span class="n">builder</span> <span class="o">=</span> <span class="n">DatasetBuilder</span><span class="p">()</span>
        <span class="n">data</span><span class="p">,</span> <span class="n">description</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_read</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">mod</span><span class="p">,</span> <span class="n">snrs</span> <span class="ow">in</span> <span class="n">description</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">snr</span> <span class="ow">in</span> <span class="n">snrs</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">iq</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[(</span><span class="n">mod</span><span class="p">,</span> <span class="n">snr</span><span class="p">)]:</span>
                    <span class="n">builder</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">iq</span><span class="o">=</span><span class="n">iq</span><span class="p">,</span> <span class="n">Modulation</span><span class="o">=</span><span class="n">mod</span><span class="p">,</span> <span class="n">SNR</span><span class="o">=</span><span class="n">snr</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">builder</span><span class="o">.</span><span class="n">build</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_download</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">urlretrieve</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">REMOTE_URL</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">CACHE_PATH</span><span class="p">)</span>
        <span class="k">with</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">CACHE_PATH</span><span class="p">,</span> <span class="s2">&quot;r:bz2&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">tar</span><span class="p">:</span>
            <span class="n">tar</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_read</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">:</span> <span class="nb">str</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Tuple</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">Dict</span><span class="p">]:</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">infile</span><span class="p">:</span>
            <span class="n">data</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">infile</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">&quot;latin&quot;</span><span class="p">)</span>

            <span class="n">description</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>
            <span class="c1"># Declare j just to get the linter to stop complaining about the lamba below</span>
            <span class="n">j</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="n">snrs</span><span class="p">,</span> <span class="n">mods</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span>
                <span class="k">lambda</span> <span class="n">j</span><span class="p">:</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="n">j</span><span class="p">],</span> <span class="n">data</span><span class="o">.</span><span class="n">keys</span><span class="p">())))),</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
            <span class="p">)</span>
            <span class="k">for</span> <span class="n">mod</span> <span class="ow">in</span> <span class="n">mods</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">snr</span> <span class="ow">in</span> <span class="n">snrs</span><span class="p">:</span>
                    <span class="n">description</span><span class="p">[</span><span class="n">mod</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">snr</span><span class="p">)</span>

            <span class="k">return</span> <span class="n">data</span><span class="p">,</span> <span class="n">description</span>


<div class="viewcode-block" id="load_RML201610A_dataset"><a class="viewcode-back" href="../../../../data.html#rfml.data.converters.rml_2016.load_RML201610A_dataset">[docs]</a><span class="k">def</span> <span class="nf">load_RML201610A_dataset</span><span class="p">(</span><span class="n">path</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Dataset</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Load the RadioML2016.10A Dataset provided by DeepSig Inc.</span>

<span class="sd">    This dataset is licensed under Creative Commons Attribution - NonCommercial -</span>
<span class="sd">    ShareAlike 4.0 License (CC BY-NC-SA 4.0) by DeepSig Inc.</span>

<span class="sd">    Args:</span>
<span class="sd">        path (str, optional): Path to the dataset which has already been downloaded from</span>
<span class="sd">                              DeepSig Inc., saved locally, and extracted (tar xjf).  If</span>
<span class="sd">                              not provided, the dataset will attempt to be downloaded</span>
<span class="sd">                              from the internet and saved locally -- subsequent calls</span>
<span class="sd">                              would read from that cached dataset that is fetched.</span>

<span class="sd">    Raises:</span>
<span class="sd">        ValueError: If *path* is provided but does not exist.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Dataset: A Dataset that has been loaded with the data from RML2016.10A</span>

<span class="sd">    License</span>
<span class="sd">        https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode</span>

<span class="sd">    Download Location</span>
<span class="sd">        https://www.deepsig.io/datasets</span>

<span class="sd">    Citation</span>
<span class="sd">        T. J. O’Shea and N. West, “Radio machine learning dataset generation with GNU</span>
<span class="sd">        Radio” in Proceedings of the GNU Radio Conference, vol. 1, 2016.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">CACHE_PATH</span> <span class="o">=</span> <span class="s2">&quot;./RML2016.10a.tar.bz2&quot;</span>
    <span class="n">WARNING_MSG</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="s2">    About to attempt downloading the RML2016.10A dataset from deepsig.io/datasets.</span>
<span class="s2">    Depending on your network connection, this process can be slow and error prone.  Any</span>
<span class="s2">    errors raised during network operations are not silenced and will therefore cause your</span>
<span class="s2">    code to crash.  If you require robustness in your experimentation, you should manually</span>
<span class="s2">    download the file locally and pass the file path to the load_RML201610a_dataset</span>
<span class="s2">    function.</span>

<span class="s2">    Further, this dataset is provided by DeepSig Inc. under Creative Commons Attribution</span>
<span class="s2">    - NonCommercial - ShareAlike 4.0 License (CC BY-NC-SA 4.0).  By calling this function,</span>
<span class="s2">    you agree to that license -- If an alternative license is needed, please contact DeepSig</span>
<span class="s2">    Inc. at info@deepsig.io</span>
<span class="s2">    &quot;&quot;&quot;</span>
    <span class="n">REMOTE_URL</span> <span class="o">=</span> <span class="s2">&quot;http://opendata.deepsig.io/datasets/2016.10/RML2016.10a.tar.bz2&quot;</span>
    <span class="n">UNPICKLED_PATH</span> <span class="o">=</span> <span class="s2">&quot;RML2016.10a_dict.pkl&quot;</span>

    <span class="n">loader</span> <span class="o">=</span> <span class="n">RML2016DataLoader</span><span class="p">(</span>
        <span class="n">cache_path</span><span class="o">=</span><span class="n">CACHE_PATH</span><span class="p">,</span>
        <span class="n">remote_url</span><span class="o">=</span><span class="n">REMOTE_URL</span><span class="p">,</span>
        <span class="n">unpickled_path</span><span class="o">=</span><span class="n">UNPICKLED_PATH</span><span class="p">,</span>
        <span class="n">warning_msg</span><span class="o">=</span><span class="n">WARNING_MSG</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">loader</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="n">path</span><span class="p">)</span></div>


<div class="viewcode-block" id="load_RML201610B_dataset"><a class="viewcode-back" href="../../../../data.html#rfml.data.converters.rml_2016.load_RML201610B_dataset">[docs]</a><span class="k">def</span> <span class="nf">load_RML201610B_dataset</span><span class="p">(</span><span class="n">path</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="kc">None</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Dataset</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Load the RadioML2016.10B Dataset provided by DeepSig Inc.</span>

<span class="sd">    This dataset is licensed under Creative Commons Attribution - NonCommercial -</span>
<span class="sd">    ShareAlike 4.0 License (CC BY-NC-SA 4.0) by DeepSig Inc.</span>

<span class="sd">    Args:</span>
<span class="sd">        path (str, optional): Path to the dataset which has already been downloaded from</span>
<span class="sd">                              DeepSig Inc., saved locally, and extracted (tar xjf).  If</span>
<span class="sd">                              not provided, the dataset will attempt to be downloaded</span>
<span class="sd">                              from the internet and saved locally -- subsequent calls</span>
<span class="sd">                              would read from that cached dataset that is fetched.</span>

<span class="sd">    Raises:</span>
<span class="sd">        ValueError: If *path* is provided but does not exist.</span>

<span class="sd">    Returns:</span>
<span class="sd">        Dataset: A Dataset that has been loaded with the data from RML2016.10B</span>

<span class="sd">    License</span>
<span class="sd">        https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode</span>

<span class="sd">    Download Location</span>
<span class="sd">        https://www.deepsig.io/datasets</span>

<span class="sd">    Citation</span>
<span class="sd">        T. J. O’Shea and N. West, “Radio machine learning dataset generation with GNU</span>
<span class="sd">        Radio” in Proceedings of the GNU Radio Conference, vol. 1, 2016.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">CACHE_PATH</span> <span class="o">=</span> <span class="s2">&quot;./RML2016.10b.tar.bz2&quot;</span>
    <span class="n">WARNING_MSG</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="s2">    About to attempt downloading the RML2016.10B dataset from deepsig.io/datasets.</span>
<span class="s2">    Depending on your network connection, this process can be slow and error prone.  Any</span>
<span class="s2">    errors raised during network operations are not silenced and will therefore cause your</span>
<span class="s2">    code to crash.  If you require robustness in your experimentation, you should manually</span>
<span class="s2">    download the file locally and pass the file path to the load_RML201610b_dataset</span>
<span class="s2">    function.</span>

<span class="s2">    Further, this dataset is provided by DeepSig Inc. under Creative Commons Attribution</span>
<span class="s2">    - NonCommercial - ShareAlike 4.0 License (CC BY-NC-SA 4.0).  By calling this function,</span>
<span class="s2">    you agree to that license -- If an alternative license is needed, please contact DeepSig</span>
<span class="s2">    Inc. at info@deepsig.io</span>
<span class="s2">    &quot;&quot;&quot;</span>
    <span class="n">REMOTE_URL</span> <span class="o">=</span> <span class="s2">&quot;http://opendata.deepsig.io/datasets/2016.10/RML2016.10b.tar.bz2&quot;</span>
    <span class="n">UNPICKLED_PATH</span> <span class="o">=</span> <span class="s2">&quot;RML2016.10b.dat&quot;</span>

    <span class="n">loader</span> <span class="o">=</span> <span class="n">RML2016DataLoader</span><span class="p">(</span>
        <span class="n">cache_path</span><span class="o">=</span><span class="n">CACHE_PATH</span><span class="p">,</span>
        <span class="n">remote_url</span><span class="o">=</span><span class="n">REMOTE_URL</span><span class="p">,</span>
        <span class="n">unpickled_path</span><span class="o">=</span><span class="n">UNPICKLED_PATH</span><span class="p">,</span>
        <span class="n">warning_msg</span><span class="o">=</span><span class="n">WARNING_MSG</span><span class="p">,</span>
    <span class="p">)</span>
    <span class="k">return</span> <span class="n">loader</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">path</span><span class="o">=</span><span class="n">path</span><span class="p">)</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Bryse Flowers

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>